package com.ibr.ticketmanagement.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.ibr.ticketmanagement.app.util.SystemUiHider;
import com.ibr.ticketmanagement.app.util.Utility;


/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 * @see SystemUiHider
 */
public class SplashScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        if (Utility.shouldRunBackgroundService(this)) {
            // Start the background service
            Intent mServiceIntent = new Intent(this, DatabaseService.class);
            mServiceIntent.setAction("com.ibr.ticketmanagement.app.SplashScreen");
            // Starts the IntentService
            startService(mServiceIntent);
            Utility.setBackgroundServiceRan(this);
            //TODO: Make a background request to check the number of tickets, etc.
        }
        new Handler().postDelayed(new Runnable() {

			/*
             * Showing splash screen with a timer. This will be useful when you
			 * want to show case your app logo / company
			 */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Utility.initDB(SplashScreen.this);
                if (Utility.isLoggedIn(getApplicationContext())) {
                    startActivity(new Intent(SplashScreen.this, ActionPlanActivity.class));
                } else {
                    startActivity(new Intent(SplashScreen.this, LoginActivity.class));
                }
                // close this activity
                finish();
            }
        }, Utility.SPLASH_TIME_OUT);
    }

}
